//
//  DYMyShopHeaderView.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/22.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "DYMyShopHeaderView.h"
#import <Masonry/Masonry.h>
#import "DYShopModel.h"
#import "UIButton+WebCache.h"

@interface DYMyShopHeaderView()
@property (nonatomic,strong) UIImageView * bgImg;
@property (nonatomic,strong) UIImageView * shopLogo;
@property (nonatomic,strong) UIImageView * vipLogo;
@property (nonatomic,strong) UILabel * shopName;
@property (nonatomic,strong) UIButton * firstGoods;
@property (nonatomic,strong) UILabel * firstValue;  //10笔
@property (nonatomic,strong) UILabel * firstTitle;  //今日交易
@property (nonatomic,strong) UIButton * secondGoods;
@property (nonatomic,strong) UILabel * secondValue; //1200RMB
@property (nonatomic,strong) UILabel * secondTitle; //今日收益
@property (nonatomic,strong) UIButton * thirdGoods;
@property (nonatomic,strong) UIImageView * thirdValue;
@property (nonatomic,strong) UILabel * thirdTitle;  //我的订单
@end


@implementation DYMyShopHeaderView

- (instancetype)init{
    if (self = [super init]) {
        _bgImg = [[UIImageView alloc] init];
        [self addSubview:self.bgImg];
        
        self.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
        
        _shopLogo = [[UIImageView alloc] initWithImage:PlaceholderImageSmall];
        _shopLogo.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.shopLogo];
        
        _vipLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VIP"]];
        [self addSubview:_vipLogo];
        
        _shopName = [UILabel new];
        _shopName.text = @"澳大利亚红酒专区";
        _shopName.textColor = [UIColor blackTextColor];
        _shopName.font = [UIFont systemFontOfSize:15];
        UITapGestureRecognizer  * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shopNameClicked)];
        _shopName.userInteractionEnabled = YES;
        [_shopName addGestureRecognizer:tapGesture];
        [self addSubview:self.shopName];
        
        _firstGoods = [UIButton buttonWithType:UIButtonTypeCustom];
                        _firstGoods.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.firstGoods];
        _firstTitle = [UILabel new];
        _firstTitle.textColor = [UIColor blackTextColor];
        _firstTitle.font = [UIFont systemFontOfSize:14];
        _firstTitle.text = @"今日交易";
        [self addSubview:self.firstTitle];
        _firstValue = [UILabel new];
        _firstValue.textColor = [UIColor redF2color];
        _firstValue.text = @"0笔";
        [self addSubview:self.firstValue];
        
        _secondGoods = [UIButton buttonWithType:UIButtonTypeCustom];
                        _secondGoods.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.secondGoods];
        _secondTitle = [UILabel new];
        _secondTitle.textColor = [UIColor blackTextColor];
        _secondTitle.font = [UIFont systemFontOfSize:14];
        _secondTitle.text = @"今日收益";
        [self addSubview:_secondTitle];
        _secondValue = [UILabel new];
        _secondValue.textColor = [UIColor redF2color];
        _secondValue.text = @"1200RMB";
        [self addSubview:self.secondValue];
        
        _thirdGoods = [UIButton buttonWithType:UIButtonTypeCustom];
                        _thirdGoods.backgroundColor = [UIColor whiteColor];
        [_thirdGoods addTarget:self action:@selector(orderClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.thirdGoods];
        _thirdTitle = [UILabel new];
        _thirdTitle.textColor = [UIColor blackTextColor];
        _thirdTitle.font = [UIFont systemFontOfSize:14];
        _thirdTitle.text = @"我的订单";
        [self addSubview:_thirdTitle];
        _thirdValue = [[UIImageView alloc] init];
        _thirdValue.image = [UIImage imageNamed:@"er_order"];
        [self addSubview:_thirdValue];
        
        [self _layout];
    }
    return self;
}

- (void)orderClicked{
    if (self.didClickedOrder) {
        self.didClickedOrder();
    }
}

- (void)loadData:(id)model{
    DYShopModel * data = [DYShopModel mj_objectWithKeyValues:model];
    [self.bgImg sd_setImageWithURL:[NSURL URLWithString:data.simg] placeholderImage:PlaceholderImageBig];
    [self.shopLogo sd_setImageWithURL:[NSURL URLWithString:data.simg] placeholderImage:PlaceholderImageSmall];
    self.shopName.text = data.sname;
    if (data.isvip) {
        self.vipLogo.hidden = NO;
    }else{
        self.vipLogo.hidden = YES;
    }
    //今日收益
    self.secondValue.text = data.sprofit;
    
    //今日交易
    self.firstValue.text = data.stransaction;
//    [self loadAD];
}
- (void)loadAD{
    __weak typeof(self) weakSelf = self;
    [MYAFNetworking POST:@"api2/getadvgoodslist" parameters:@{@"apid":@9} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf animated:YES];
        NSString * code = [responseObject safeObjectForKey:@"code"];
        if ([code isEqualToString:@"200"]) {
            NSArray * data = [responseObject safeObjectForKey:@"data"];
            if (data.count > 0) {
                NSString * imgUrl = [data[0] safeObjectForKey:@"aimg"];
                [self.firstGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
            } else {
                [self.firstGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
            }
            if (data.count > 1) {
                NSString * imgUrl = [data[1] safeObjectForKey:@"aimg"];
                [self.secondGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
            } else {
                [self.secondGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
            }
            if (data.count > 2) {
                NSString * imgUrl = [data[2] safeObjectForKey:@"aimg"];
                [self.thirdGoods sd_setBackgroundImageWithURL:[NSURL URLWithString:imgUrl] forState:UIControlStateNormal placeholderImage:PlaceholderImageBig];
            } else {
                [self.thirdGoods setBackgroundImage:PlaceholderImageBig forState:UIControlStateNormal];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf animated:YES];
    }];
}

- (void)_layout{
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.equalTo(@175);
    }];
    
    [self.shopLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self).offset(80);
        make.width.equalTo(@60);
        make.height.equalTo(@21);
    }];
    [self.vipLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopLogo);
        make.top.equalTo(self.shopLogo.mas_bottom).offset(4);
    }];
    [self.shopName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopLogo.mas_right).offset(5);
        make.centerY.equalTo(self.shopLogo);
        make.right.equalTo(self).offset(-100);
    }];
    [self.firstGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self).offset(180);
        make.width.equalTo(self).multipliedBy(.33);
        make.height.equalTo(@110);
    }];
    [self.firstValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.firstGoods);
        make.bottom.equalTo(self.firstGoods.mas_centerY).offset(-5);
    }];
    [self.firstTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.firstGoods);
        make.top.equalTo(self.firstGoods.mas_centerY).offset(5);
    }];
    
    [self.secondGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firstGoods);
        make.height.equalTo(@110);
        make.center.equalTo(self);
        make.width.equalTo(self).multipliedBy(.34);
    }];
    [self.secondValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.secondGoods);
        make.bottom.equalTo(self.secondGoods.mas_centerY).offset(-5);
    }];
    [self.secondTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.secondGoods);
        make.top.equalTo(self.secondGoods.mas_centerY).offset(5);
    }];
    [self.thirdGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firstGoods);
        make.height.equalTo(@110);
        make.right.equalTo(self);
        make.width.equalTo(self).multipliedBy(.33);
    }];
    [self.thirdValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.thirdGoods);
        make.bottom.equalTo(self.thirdGoods.mas_centerY).offset(-5);
    }];
    [self.thirdTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.thirdGoods);
        make.top.equalTo(self.thirdGoods.mas_centerY).offset(5);
    }];

}

- (void)shopNameClicked{
    NSLog(@"点击商店名字");
    if (self.didClickedShopName) {
        self.didClickedShopName();
    }
}

@end
